﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using DBUtility;
/// <summary>
/// AspNetPager 的摘要说明
/// </summary>
public class AspNetPager
{
	/// <summary>
        /// 获取记录集总数
        /// </summary>
        /// <returns></returns>
    public int GetRecordCount(AspNetPagerInfo info)
    {
        SqlParameter[] parameters = SqlHelper.GetCachedParameters("Psp_SetPagination");
        if (parameters == null)
        {
            parameters = new SqlParameter[] { 
                    new SqlParameter("@vc_Table",SqlDbType.VarChar,50),
                    new SqlParameter("@vc_SqlWhere",SqlDbType.VarChar,1000),
                    new SqlParameter("@vc_SqlOrder",SqlDbType.VarChar,1000),
                    new SqlParameter("@vc_SqlFile",SqlDbType.VarChar,1000),
                    new SqlParameter("@vc_Key",SqlDbType.VarChar,50),
                    new SqlParameter("@vc_FieldType",SqlDbType.VarChar,50),
                    new SqlParameter("@i_PageSize",SqlDbType.Int),
                    new SqlParameter("@i_PageIndex",SqlDbType.Int),
                    new SqlParameter("@bt_Count",SqlDbType.Bit)
                };

            SqlHelper.CacheParameters("Psp_SetPagination", parameters);
        }

        parameters[0].Value = info.TableName;
        parameters[1].Value = info.SqlWhere;
        parameters[2].Value = info.SqlOrder;
        parameters[3].Value = info.SqlFile;
        parameters[4].Value = info.KeyField;
        parameters[5].Value = info.FieldType;
        parameters[6].Value = info.PageSize;
        parameters[7].Value = info.PageIndex;
        parameters[8].Value = 1;

        return (int)SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocal, CommandType.StoredProcedure, "Psp_SetPagination", parameters);
    }

    /// <summary>
    /// 获取分页后列表
    /// </summary>
    /// <param name="info"></param>
    /// <returns></returns>
    public SqlDataReader GetListForDataReader(AspNetPagerInfo info)
    {
        SqlParameter[] parameters = SqlHelper.GetCachedParameters("Psp_SetPagination");
        if (parameters == null)
        {
            parameters = new SqlParameter[] { 
                    new SqlParameter("@vc_Table",SqlDbType.VarChar,50),
                    new SqlParameter("@vc_SqlWhere",SqlDbType.VarChar,1000),
                    new SqlParameter("@vc_SqlOrder",SqlDbType.VarChar,1000),
                    new SqlParameter("@vc_SqlFile",SqlDbType.VarChar,1000),
                    new SqlParameter("@vc_Key",SqlDbType.VarChar,50),
                    new SqlParameter("@vc_FieldType",SqlDbType.VarChar,50),
                    new SqlParameter("@i_PageSize",SqlDbType.Int),
                    new SqlParameter("@i_PageIndex",SqlDbType.Int),
                    new SqlParameter("@bt_Count",SqlDbType.Bit)
                };

            SqlHelper.CacheParameters("Psp_SetPagination", parameters);
        }

        parameters[0].Value = info.TableName;
        parameters[1].Value = info.SqlWhere;
        parameters[2].Value = info.SqlOrder;
        parameters[3].Value = info.SqlFile;
        parameters[4].Value = info.KeyField;
        parameters[5].Value = info.FieldType;
        parameters[6].Value = info.PageSize;
        parameters[7].Value = info.PageIndex;
        parameters[8].Value = 0;

        return SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.StoredProcedure, "Psp_SetPagination", parameters);
    }

    public DataSet GetListForDataSet(AspNetPagerInfo info)
    {
        SqlParameter[] parameters = SqlHelper.GetCachedParameters("Psp_SetPagination");
        if (parameters == null)
        {
            parameters = new SqlParameter[] { 
                    new SqlParameter("@vc_Table",SqlDbType.VarChar,50),
                    new SqlParameter("@vc_SqlWhere",SqlDbType.VarChar,1000),
                    new SqlParameter("@vc_SqlOrder",SqlDbType.VarChar,1000),
                    new SqlParameter("@vc_SqlFile",SqlDbType.VarChar,1000),
                    new SqlParameter("@vc_Key",SqlDbType.VarChar,50),
                    new SqlParameter("@vc_FieldType",SqlDbType.VarChar,50),
                    new SqlParameter("@i_PageSize",SqlDbType.Int),
                    new SqlParameter("@i_PageIndex",SqlDbType.Int),
                    new SqlParameter("@bt_Count",SqlDbType.Bit)
                };

            SqlHelper.CacheParameters("Psp_SetPagination", parameters);
        }

        parameters[0].Value = info.TableName;
        parameters[1].Value = info.SqlWhere;
        parameters[2].Value = info.SqlOrder;
        parameters[3].Value = info.SqlFile;
        parameters[4].Value = info.KeyField;
        parameters[5].Value = info.FieldType;
        parameters[6].Value = info.PageSize;
        parameters[7].Value = info.PageIndex;
        parameters[8].Value = 0;

        return SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringLocal, CommandType.StoredProcedure, "Psp_SetPagination", parameters);
    }
}
